package com.kkd.kkdapp.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.RegularAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.BabyData;
import com.kkd.kkdapp.http.entity.HomeTender;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.listener.OnSingleClickListener;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.Time_Date;
import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

public class AllTheBidActivity extends BaseActivity implements OnClickListener,
		KKDScrollListener {

	private final int NOMORE = 1; // 当前页数大于总页数
	private final int REFRESH = 2; // 当前页数小于总页数
	private final int REQUEST_WAS_ABORTED = 3; // 请求失败
	private RelativeLayout title_img; // 返回
	private ImageView img_null; // 空值或网络异常图片
	private TextView tv_null; // 空值或网络异常提示语
	private TextView title_tv;
	private RegularAdapter adapter;
	private KkdXlistView xlistview;
	private ListView mListView;
	private int pageNO = 1;
	private int pageSize = 20;
	private BabyData mBabyData;
	private ArrayList<HomeTender> datalist = new ArrayList<HomeTender>();
	private boolean REFRESHFLAG = true;
	private int productType;
	private String bidTitle;
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			String time = "";
			switch (msg.what) {
			case NOMORE:
				xlistview.stopLoadMore(-1);
				if (REFRESHFLAG) {
					xlistview.stopLoadMore(1);
				}
				break;
			case REFRESH:
				if (datalist.size() > 0) {
					adapter.notifyKkdDataSetChanged();
					if (mBabyData.getData().size() < pageSize) {
						xlistview.stopLoadMore(1);
					} else {
						xlistview.stopLoadMore(0);
					}
					xlistview.stopRefresh();
					Time_Date time_Date = new Time_Date();
					time = time_Date.get_Year_Day();
					xlistview.setRefreshTime(time);
					xlistview.setVisibility(View.VISIBLE);
					tv_null.setVisibility(View.GONE);
					img_null.setVisibility(View.GONE);
				} else {
					img_null.setImageResource(R.drawable.img_null);
					img_null.setVisibility(View.VISIBLE);
					tv_null.setText("新的"+bidTitle+"正在准备中，请稍后再来");
					
					tv_null.setVisibility(View.VISIBLE);
					xlistview.stopLoadMore(0);
					xlistview.stopRefresh();
					Time_Date time_Date = new Time_Date();
					time = time_Date.get_Year_Day();
					xlistview.setRefreshTime(time);
				}

				break;
			case REQUEST_WAS_ABORTED:
				// showToast("加载失败");
				xlistview.stopLoadMore(1);
				xlistview.stopRefresh();
				break;
			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_all_the_bid);
		bidTitle = getIntent().getExtras().getString(
				"bidTitle");
		productType = getIntent().getExtras().getInt(
				"productType", 0);
		initView(); // 初始化控件
		setListener(); // 注册监听时间
		whetherNetWork(); // 是否有网络
	}

	/****************
	 * 初始化控件
	 ****************/
	public void initView() {
		title_tv = (TextView) this.findViewById(R.id.title_tv);
		title_img = (RelativeLayout) this.findViewById(R.id.title_img);
		img_null = (ImageView) this.findViewById(R.id.img_null);
		tv_null = (TextView) this.findViewById(R.id.tv_null);
		xlistview = (KkdXlistView) this.findViewById(R.id.xlistview);
		xlistview.setFooterTip("如需查看更早的标的,请前往我的定期投资查看>>");
		xlistview.getFooter().setOnClickListener(new OnSingleClickListener() {
			
			@Override
			public void onSingleClick(View v) {
				if(((TextView)v).getText().toString().equals("如需查看更早的标的,请前往我的定期投资查看>>")){
					Intent intent = new Intent(AllTheBidActivity.this,MyRegularDebtActivity.class);
					startActivity(intent);
				}
			}
		});
		mListView = xlistview.mListView;
		mListView.setDividerHeight(0);
		title_tv.setText(bidTitle);
	}

	/***************
	 * 注册监听时间
	 ***************/
	public void setListener() {
		title_img.setOnClickListener(this);
		xlistview.setKKDScrollListener(this); // 注册下拉刷新和加载更多
	}

	public void whetherNetWork() {
		xlistview.startRefresh();

		setMyAdater();

		if (!AppUtil.isConnect(this)) {
			xlistview.stopRefresh();
			img_null.setVisibility(View.VISIBLE);
			tv_null.setVisibility(View.VISIBLE);
		}
	}

	/************
	 * 适配器
	 ************/
	public void setMyAdater() {
		adapter = new RegularAdapter(this, datalist);
		mListView.setAdapter(adapter);

		requestAllBid(); // 请求接口
	}
	/************
	 * 请求标的
	 ************/
	public void requestAllBid() {
		
		KkdHttpMagager.getInstance().getAllBidList(pageNO, pageSize,productType,
				BabyData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						mBabyData = (BabyData) responseBody;
						int pageCount = mBabyData.getPageCount();
						int nPageNo = mBabyData.getPageNO();
						if (nPageNo > pageCount && nPageNo != pageNO) {
							mHandler.sendEmptyMessage(NOMORE);
						} else {
							xlistview.setNoMore(false);
							if (pageNO == 1) {
								datalist.clear();
							}
							datalist.addAll(mBabyData.getData());
							mHandler.sendEmptyMessage(REFRESH);
						}
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						mHandler.sendEmptyMessage(REQUEST_WAS_ABORTED);
					}
				});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_img:
			finish();
			break;
		}

	}

	/****************
	 * 下拉刷新
	 ****************/
	@Override
	public void onRefresh() {
		pageNO = 1;
		adapter.mIsFirstAnimation = true;
		requestAllBid();
		REFRESHFLAG = true;
	}

	/***************
	 * 加载更多
	 ***************/
	@Override
	public void onLoadMore() {
		pageNO++;
		requestAllBid();
		REFRESHFLAG = false;
	}

}